/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.dao.CampaignDao;
import cn.com.recloud.bbs.dto.CampaignAnalysisDTO;
import cn.com.recloud.bbs.entity.bbs_campaign;
import cn.com.recloud.bbs.entity.bbs_campaign_sign_up;
import cn.com.recloud.bbs.dao.CampaignSignUpDao;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.CampaignService;
import cn.com.recloud.bbs.service.CampaignSignUpService;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.util.UserUtil;
import cn.com.recloud.bbs.vo.SharedLogVO;
import cn.com.recloud.core.constant.SysConstant;
import cn.com.recloud.one.spring.exception.OneException;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import cn.com.recloud.one.spring.util.ExportUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author kim dong
 * @data 2020-06-08
 */
@Service
public class CampaignSignUpServiceImpl extends BaseServiceImpl<CampaignSignUpDao, bbs_campaign_sign_up> implements CampaignSignUpService {
    @Autowired
    private  CampaignSignUpDao signUpDao;
    @Autowired
    CampaignDao campaignDao;
    @Resource
    private MetaoptionService metaoptionService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 获取活动报名的数据
     * @param dto
     * @return
     */
    @Override
    public Map serchCampaignSignUp(CampaignAnalysisDTO dto) {
        Map map = new HashMap();
        dto.setPageIndex((dto.getPageIndex()-1) * dto.getPageSize());
        List<SharedLogVO> list = signUpDao.listCampaignSignUp(dto);
        Integer count = signUpDao.listCountCampaignSignUp(dto);
        map.put("list",list);
        map.put("count",count);
        return map;
    }

    /**
     * 导出活动报名的详细列表
     * @param campaign
     * @param response
     */
    @Override
    public void exportCampaignUpAnalysis(CampaignAnalysisDTO campaign, HttpServletResponse response) {
        List<SharedLogVO> signUps = signUpDao.exportSignUp(campaign);
        ExportUtil<SharedLogVO> exportUtil = new ExportUtil<>();
        exportUtil.export(response,signUps,campaign.getExportMap(),campaign.getExportName());
    }

    /**
     * 活动报名
     * @param bbsCampaignSignUp
     * @return
     */
    @Override
    public boolean signUpCampaign(bbs_campaign_sign_up bbsCampaignSignUp) {
        //判断活动是否过期，过期不能报名
        bbs_campaign bbsCampaign = campaignDao.selectById(bbsCampaignSignUp.getBbs_campaignid());
        //当前时间
        Date nowTime = DateUtil.date(Calendar.getInstance());
        if(bbsCampaign.getEnd_time() != null){
            //活动结束时间
            DateTime endTime = new DateTime(bbsCampaign.getEnd_time());
            //结束时间在当前时间之前
            if(endTime.before(nowTime)){
                throw new OneException("活动已结束");
            }
        }
        String userId = userUtil.getUserId();
        String userName = userUtil.getUserName();
        metaoption metaoption = null;
        //未登录就是联系人，登录则是系统用户
        if(SysConstant.DEFAULT_ID.equals(userId)){
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"1");
        }else{ //用户登录
            metaoption = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.SIGNUP_TYPE,"2");
        }
        bbsCampaignSignUp.setSign_up_by_type(metaoption.getValue());
        bbsCampaignSignUp.setSign_up_by_typename(metaoption.getName());
        bbsCampaignSignUp.setSign_up_by_id(userId);
        bbsCampaignSignUp.setSign_up_by_name(userName);
        return super.save(bbsCampaignSignUp);
    }

}
